let express = require('express');
let mysql = require('./db');
let multer = require('multer');
let path = require('path')
let router = express.Router();

//配置文件上传的详细信息
let storage = multer.diskStorage({
    destination: './src/upload',
    filename: function (req, file, cb) {
        // req:请求对象
        // file: 上传文件信息
        // cb：回调函数，用于确认文件名称

        // 获取文件的后缀名
        let ext = path.extname(file.originalname)
        cb(null, 'abc' + Date.now() + ext)
    }
})

let upload = multer({
    storage: storage
})


//上传图片
router.post('/upload', upload.single('dishes_img'), async (req, res) => {
    let { dishes_id } = req.body;

    let img = req.file  ?  'http://116.62.185.51:2202/upload/' + req.file.filename : 'http://116.62.185.51:2202/upload/abc1665978712925.webp';
    console.log('file',img);
    mysql.query(`update menu set dishes_img='${img}' where dishes_id=${dishes_id};`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '修改数据成功',
                img
            })
        } else {
            res.send({
                code: 400,
                msg: '修改数据失败'
            })
        }
    })
})

//添加菜品
router.post('/addDishes', function (req, res) {
    let { dishes_name, dishes_qty, price, sales_price, dishes_img, category, online } = req.body
    online = online || true
    sales_price = sales_price ||  null
    dishes_img = dishes_img || 'http://116.62.185.51:2202/upload/abc1665978712925.webp'
    mysql.query(`insert into menu(dishes_id,dishes_name,category,dishes_qty,price,sales_price,dishes_img,online) values(Null,'${dishes_name}','${category}','${dishes_qty}','${price}','${sales_price}','${dishes_img}',${online});`, (state, data) => {
        if (state) {
            console.log(data.insertId);
            res.send({
                code: 200,
                msg: '菜品添加成功',
                data
            })

        } else {
            res.send({
                code: 400,
                msg: '菜品添加失败',
                data
            })
        }
    })
})

//删除菜品
router.post('/delDishes', function (req, res) {
    let { dishes_id } = req.body
    mysql.query(`delete from menu where dishes_id='${dishes_id}';`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '菜品移除成功',
                data
            })

        } else {
            res.send({
                code: 400,
                msg: '菜品移除失败',
                data
            })
        }
    })
})

//批量删除菜品
router.post('/delSome', function (req, res) {
    let { ids } = req.body
    // let dishes_ids =  ids.join(',')
    mysql.query(`delete from menu where dishes_id in (${ids});`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '菜品移除成功',
                data
            })

        } else {
            res.send({
                code: 400,
                msg: '菜品移除失败',
                data
            })
        }
    })
})

//修改菜品
router.post('/updateDishes', function (req, res) {
    let { dishes_id, dishes_name, dishes_qty, price, sales_price, dishes_img, category, online } = req.body
    console.log(req.body);
    online = online ? 1 : 0
    sales_price = sales_price || null
    dishes_img = dishes_img || 'http://116.62.185.51:2202/upload/abc1665978712925.webp'
    mysql.query(`update menu set dishes_name='${dishes_name}', dishes_qty='${dishes_qty}' ,category='${category}',price='${price}', sales_price=${sales_price},dishes_img='${dishes_img}',online=${online} where dishes_id='${dishes_id}';`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '菜品修改成功',
                data
            })

        } else {
            res.send({
                code: 400,
                msg: '菜品修改失败',
                data
            })
        }
    })
})
0
//查询所有菜品
router.get('/selAlldishes', (req, res) => {
    let { page, size } = req.query
    // 当不传递 page 和 limit 需要设置默认值
    page = page || 1;
    size = size || 20;
    let index = (page - 1) * size;
    mysql.query(`select * from menu limit ${index},${size};`, (state, data) => {
        if (state) {
            mysql.query(`select count(*) as total from menu;`, (state1, total) => {
                if (state1) {
                    res.send({
                        code: 200,
                        msg: '请求成功',
                        total: total[0].total,
                        data
                    })
                } else {
                    res.send({
                        code: 400,
                        msg: '请求错误',
                        data
                    })
                }
            })
        } else {
            res.send({
                code: 400,
                msg: '请求错误',
                data
            })
        }
    })
})

//根据指定分类查询所有菜品
router.get('/selCategory', (req, res) => {
    let { category } = req.query
    mysql.query(`select * from menu where category='${category}';`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '请求成功',
                data
            })
        } else {
            res.send({
                code: 400,
                msg: '请求错误',
                data
            })
        }
    })
})

//根据id查询指定数据
router.get('/selID', (req, res) => {
    let { dishes_id } = req.query
    mysql.query(`select * from menu where dishes_id='${dishes_id}';`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '请求成功',
                data: data[0]
            })
        } else {
            res.send({
                code: 400,
                msg: '请求错误',
                data
            })
        }
    })
})

module.exports = router